
#define CurrentID ((get_group_id(1)*get_num_groups(1)) + get_group_id(0))

//#define M_PI 3.1415926535897
#define M_2PI 6.2831853071795


//#define FLT_MAX         3.402823466e+38F
//#define FLT_MIN         1.175494351e-38F


typedef struct {
    uint    m_Frequency;    //频率
    float   m_Intensity;    //强度
    int     m_WaveOffset;   //波形偏移
    uint    m_EnableTracks;   //波形偏移
}S_SWData;


__kernel void f_SoundWavesGenerate( __global S_SWData* m_Waves, 
                                    __global short* SoundBuf, 
                                    uint m_Sampling, 
                                    uint Second,
                                    uint NumTracks,
                                    float m_Gain) {
	const size_t id = CurrentID;
	
    if(id < m_Sampling*Second){
        float sp = 0.0;

        for(uint i=0; i<NumTracks; ++i){
            if(m_Waves[i].m_EnableTracks > 0){

                float tmp_sp = M_2PI / ((float)m_Sampling / (float)m_Waves[i].m_Frequency);
                float offset = m_Waves[i].m_WaveOffset + id;
                sp += sin(tmp_sp * offset) * m_Waves[i].m_Intensity;
            }
            
        }
        
        SoundBuf[id] = (sp * m_Gain) * 100;
        //SoundBuf[id] = 1024;

        //printf("data = %f \n", m_Gain);
	}

}


